using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using ALib;
using ALib.Data.IO;
using ALib.Misc;

namespace TradingPlatform
{
    public partial class UtilityGui : Form
    {
        private readonly TradingKernel _tradingKernel;

        public UtilityGui(TradingKernel tradingKernel)
        {
            InitializeComponent();

            _tradingKernel = tradingKernel;

            _tradingKernel.OnDisplayStatus += new TradingKernel.TradingGuiDisplayEventHandler(_tradingKernel_OnDisplayStatus);
            _tradingKernel.OnDisplayErrorMessage += new TradingKernel.TradingGuiDisplayEventHandler(_tradingKernel_OnDisplayErrorMessage);
            
            string trdingClientName = _tradingKernel != null ? _tradingKernel.TradingClientName : string.Empty;
            Text = "TradingClient:" + trdingClientName;
        }

        private void buttonConnectTradingApi_Click(object sender, EventArgs e)
        {
            _tradingKernel.ConnectTradingApi();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            _tradingKernel.StartTimerBasedTrading();
        }

        protected void DisplayStatus(string outputString)
        {
            listBoxStatus.BeginUpdate();
            listBoxStatus.Items.Add(DateTime.Now.ToString(DateTimeFormatType.Complete) + "\t" + outputString);
            if (listBoxStatus.Items.Count > 1000)
            {
                listBoxStatus.Items.RemoveAt(0);
            }
            listBoxStatus.EndUpdate();
        }

        protected void DisplayErrorMessage(string outputString)
        {
            listBoxErrorMessage.BeginUpdate();
            listBoxErrorMessage.Items.Add(DateTime.Now.ToString(DateTimeFormatType.Complete) + "\t" + outputString);
            if (listBoxErrorMessage.Items.Count > 1000)
            {
                listBoxErrorMessage.Items.RemoveAt(0);
            }
            listBoxErrorMessage.EndUpdate();
        }

        void _tradingKernel_OnDisplayStatus(string outputString)
        {
            DisplayStatus(outputString);
        }

        void _tradingKernel_OnDisplayErrorMessage(string outputString)
        {
            DisplayErrorMessage(outputString);
        }
    }
}